SWEP.Base = "weapon_ic_baseweapon"

if SERVER then
	AddCSLuaFile ("shared.lua")
end

SWEP.Spawnable				= false
SWEP.AdminSpawnable			= false

SWEP.PrintName			= "STICK BASE"
SWEP.Description		= ""

SWEP.Slot					= 3
SWEP.IconFont				= "HL2MPTypeDeath"
SWEP.IconLetter				= "!"

SWEP.Primary.ClipSize		= -1
SWEP.Primary.DefaultClip	= 101
SWEP.Primary.Automatic		= false
SWEP.Primary.Ammo			= "Battery"

SWEP.Primary.Sound			= Sound ("Airboat_fan_idle")

SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic	= false 
SWEP.Secondary.Ammo			= "none"

SWEP.FiresBullets			= false


SWEP.ViewModel = "models/weapons/v_stunbaton.mdl"
SWEP.ViewModelFlip = false
SWEP.ViewModelDefPos = Vector (-8.4983, -7.4244, -1.2138)
SWEP.ViewModelDefAng = Vector (17.8752, -7.8559, 0)
SWEP.ViewModelFirePos = Vector (-16.4289, -3.4244, -1.1541)
SWEP.ViewModelFireAng = Vector (14.2404, -12.0996, -31.4525)

SWEP.WorldModel = "models/weapons/w_stunbaton.mdl"
SWEP.HoldType = "melee"

SWEP.ActiveToggleDelay = 0.1

SWEP.ActivationTime = 0
SWEP.DeactivationTime = 0

SWEP.HealDelay = 0.2
SWEP.HealAmount = 50

SWEP.DoNotRestock = true

SWEP.RechargeDelay = 2
SWEP.RechargeAmount = 5
SWEP.RechargeUpto = 100

//if CLIENT then killicon.AddFont("weapon_ic_healstick", SWEP.IconFont, SWEP.IconLetter, Color(255, 80, 0, 255)) end



function SWEP:PrimaryAttack()
	if ( self:Ammo() <= 0 ) then return end // Out of ammo.
	self:SetOn() // Toggle.	
	self:SetNextPrimaryFire( CurTime() + self.ActiveToggleDelay )
end
function SWEP:Think()
	if ( self:Ammo() <= 0 ) then
		self:SetOn( false )
	end
	
	if ( CLIENT ) then return end
	
	self:Recharge()
	
	if ( self:Ammo() > 0 ) then
		if ( self:GetNWBool( "on" ) == false ) then return end
		if ( CurTime() < ( self.NextHeal or 0 ) ) then return else self.NextHeal = CurTime() + self.HealDelay end
	
		self:DoFire()
	end
end
function SWEP:Ammo()
	return self.Owner:GetAmmoCount( self:GetPrimaryAmmoType() ) - 1
end

function SWEP:Recharge()
	if ( CLIENT ) then return end
	
	self.NextRecharge = self.NextRecharge or CurTime()
	if ( CurTime() > self.NextRecharge ) then
		local time = math.floor( ( CurTime() - self.NextRecharge ) / self.RechargeDelay ) + 1
		local recharge = math.min( self.RechargeUpto - self:Ammo(), self.RechargeAmount * time )
		
		self.Owner:GiveAmmo( recharge, self:GetPrimaryAmmoType(), true )
		
		self.NextRecharge = CurTime() + self.RechargeDelay
	end
end

function SWEP:Deploy()
	self:Recharge()
end

// Stop on holster/remove.
function SWEP:Holster()
	self:SetOn( false )
	return true
end
function SWEP:OnRemove()
	self:SetOn( false )
end

function SWEP:SetOn( bool )
	if ( bool == nil ) then bool = !self:IsOn() end
	
	self:SetNWBool( "on", bool )
	
	if ( bool ) then
		local beam = EffectData()
			beam:SetEntity( self.Owner )
			beam:SetAttachment( 1 )
			beam:SetAngles(Angle(self.BeamColour.r, self.BeamColour.g, self.BeamColour.b))
		util.Effect( "inc_healtracer", beam, true, true )
		
		if ( IsValid( self.Owner ) ) then self.Owner:EmitSound( self.Primary.Sound ) end
	else
		if ( IsValid( self.Owner ) ) then self.Owner:StopSound( self.Primary.Sound ) end
	end
end
function SWEP:IsOn() // I got on your mother! OH SNAP.
	return self:GetNWBool( "on" )
end

// Dev's fancy code - leave this alone.
function SWEP:GetViewModelPosition (pos, ang, mul)
	if self:IsOn() then
		self.DeactivationTime = nil
		self.ActivationTime = self.ActivationTime or CurTime()
		mul = math.Clamp ((CurTime() - self.ActivationTime) / self.ActiveToggleDelay, 0, 1)
	else
		self.ActivationTime = nil
		self.DeactivationTime = self.DeactivationTime or CurTime()
		mul = math.Clamp (1 - (CurTime() - self.DeactivationTime) / self.ActiveToggleDelay, 0, 1)
	end
	
		--this is always applied
		local DefPos = self.ViewModelDefPos
		local DefAng = self.ViewModelDefAng
		
		if DefAng then
			ang = ang * 1
			ang:RotateAroundAxis (ang:Right(), 		DefAng.x)
			ang:RotateAroundAxis (ang:Up(), 		DefAng.y)
			ang:RotateAroundAxis (ang:Forward(), 	DefAng.z)
		end

		if DefPos then
			local Right 	= ang:Right()
			local Up 		= ang:Up()
			local Forward 	= ang:Forward()
		
			pos = pos + DefPos.x * Right
			pos = pos + DefPos.y * Forward
			pos = pos + DefPos.z * Up
		end
		
		local AddPos = self.ViewModelFirePos - self.ViewModelDefPos
		local AddAng = self.ViewModelFireAng - self.ViewModelDefAng
		
		if AddAng and mul > 0 then
			ang = ang * 1
			ang:RotateAroundAxis (ang:Right(), 		AddAng.x * mul)
			ang:RotateAroundAxis (ang:Up(), 		AddAng.y * mul)
			ang:RotateAroundAxis (ang:Forward(), 	AddAng.z * mul)
		end
		
		if AddPos and mul > 0 then
			local Right 	= ang:Right()
			local Up 		= ang:Up()
			local Forward 	= ang:Forward()
		
			pos = pos + AddPos.x * Right * mul
			pos = pos + AddPos.y * Forward * mul
			pos = pos + AddPos.z * Up * mul
		end
	
	return pos, ang
end
